package com.example.demo.service.impl;

import com.example.demo.Mapper.UserMapper;
import com.example.demo.model.UserExample;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User userLogin(String name, String password) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andUserNameEqualTo(name).andUserPwdEqualTo(password);

        List<User> users=userMapper.selectByExample(userExample);
        if(users.size()==0||users.get(0).getDeleteFlag().equals("1"))
        {
            return null;
        }
        return users.get(0);
    }

    @Override
    public List<User> selectByCondition(String id, String name, String phone, String email,String deleteFlag) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        if (id!=null)
            criteria.andUserIdEqualTo(Integer.parseInt(id));
        if(name!=null)
            criteria.andUserNameEqualTo(name);
        if(phone!=null)
            criteria.andUserPhoneEqualTo(phone);
        if(email!=null)
            criteria.andUserEmailEqualTo(email);
        if (deleteFlag!=null)
            criteria.andDeleteFlagEqualTo(deleteFlag);

        return userMapper.selectByExample(userExample);
    }

    @Override
    public List<User> selectByCondition(String id, String name, String phone, String email) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        if (id!=null)
            criteria.andUserIdEqualTo(Integer.parseInt(id));
        if(name!=null)
            criteria.andUserNameEqualTo(name);
        if(phone!=null)
            criteria.andUserPhoneEqualTo(phone);
        if(email!=null)
            criteria.andUserEmailEqualTo(email);
        criteria.andDeleteFlagEqualTo("0");

        return userMapper.selectByExample(userExample);
    }


    @Override
    @Transactional
    public int userUpdate(User user) {
        return userMapper.updateByPrimaryKeySelective(user);    }

    @Override
    @Transactional
    public int userDelete(User user) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();

        if (user.getUserId()!=null)
            criteria.andUserIdEqualTo(user.getUserId());
        if(user.getUserName()!=null)
            criteria.andUserNameEqualTo(user.getUserName());
        if(user.getUserPhone()!=null)
            criteria.andUserPhoneEqualTo(user.getUserPhone());
        if(user.getUserEmail()!=null)
            criteria.andUserEmailEqualTo(user.getUserEmail());

        user.setDeleteFlag("1");
        return userMapper.updateByExampleSelective(user,userExample);
    }

    @Override
    @Transactional
    public int userInsert(User user) {
        return userMapper.insertSelective(user);
    }
    
}
